/*
 * ResultFrame.java
 *
 * Created on 31.05.2012, 18:15:05
 */
package treepro;

import calcsdk.nw.Patch;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Ivan
 */
public class ResultFrame extends javax.swing.JFrame {

    public void clearResults() {
        jTextArea1.setText("");
        //areaError.setText("");
        areaReport.setText("");
        areaLoop.setText("");
        labelPathOut.setText(" из 0");
    }
    
    /** Creates new form ResultFrame */
    public ResultFrame() {
        initComponents();
    }
    
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel5 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        labelPathOut = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        areaReport = new javax.swing.JTextArea();
        jPanel8 = new javax.swing.JPanel();
        jButton9 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        areaLoop = new javax.swing.JTextArea();
        jPanel7 = new javax.swing.JPanel();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        labelPathOut1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        areaError = new javax.swing.JTextArea();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(treepro.TreeProApp.class).getContext().getResourceMap(ResultFrame.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setAlwaysOnTop(true);
        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jToggleButton1.setSelected(true);
        jToggleButton1.setText(resourceMap.getString("jToggleButton1.text")); // NOI18N
        jToggleButton1.setName("jToggleButton1"); // NOI18N
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToggleButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 315, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton4)
                    .addComponent(jToggleButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setLayout(new java.awt.BorderLayout());

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setFont(resourceMap.getFont("jTextArea1.font")); // NOI18N
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane2.setViewportView(jTextArea1);

        jPanel3.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel5.setName("jPanel5"); // NOI18N

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        labelPathOut.setText(resourceMap.getString("labelPathOut.text")); // NOI18N
        labelPathOut.setName("labelPathOut"); // NOI18N

        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelPathOut)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 262, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton2)
                    .addComponent(jButton5)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPathOut))
                .addContainerGap())
        );

        jPanel3.add(jPanel5, java.awt.BorderLayout.PAGE_END);

        jTabbedPane1.addTab(resourceMap.getString("jPanel3.TabConstraints.tabTitle"), jPanel3); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        areaReport.setColumns(20);
        areaReport.setFont(resourceMap.getFont("areaReport.font")); // NOI18N
        areaReport.setRows(5);
        areaReport.setName("areaReport"); // NOI18N
        jScrollPane1.setViewportView(areaReport);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel8.setName("jPanel8"); // NOI18N

        jButton9.setText(resourceMap.getString("jButton9.text")); // NOI18N
        jButton9.setName("jButton9"); // NOI18N
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                .addContainerGap(416, Short.MAX_VALUE)
                .addComponent(jButton9)
                .addContainerGap())
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton9)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.add(jPanel8, java.awt.BorderLayout.PAGE_END);

        jTabbedPane1.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        jPanel6.setName("jPanel6"); // NOI18N
        jPanel6.setLayout(new java.awt.BorderLayout());

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        areaLoop.setColumns(20);
        areaLoop.setFont(resourceMap.getFont("areaLoop.font")); // NOI18N
        areaLoop.setRows(5);
        areaLoop.setName("areaLoop"); // NOI18N
        jScrollPane4.setViewportView(areaLoop);

        jPanel6.add(jScrollPane4, java.awt.BorderLayout.CENTER);

        jPanel7.setName("jPanel7"); // NOI18N

        jButton6.setText(resourceMap.getString("jButton6.text")); // NOI18N
        jButton6.setName("jButton6"); // NOI18N
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText(resourceMap.getString("jButton7.text")); // NOI18N
        jButton7.setName("jButton7"); // NOI18N
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setText(resourceMap.getString("jButton8.text")); // NOI18N
        jButton8.setName("jButton8"); // NOI18N
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        labelPathOut1.setText(resourceMap.getString("labelPathOut1.text")); // NOI18N
        labelPathOut1.setName("labelPathOut1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelPathOut1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 213, Short.MAX_VALUE)
                .addComponent(jButton6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton7)
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton7)
                    .addComponent(jButton6)
                    .addComponent(jButton8)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPathOut1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel6.add(jPanel7, java.awt.BorderLayout.PAGE_END);

        jTabbedPane1.addTab(resourceMap.getString("jPanel6.TabConstraints.tabTitle"), jPanel6); // NOI18N

        jPanel4.setName("jPanel4"); // NOI18N
        jPanel4.setLayout(new java.awt.BorderLayout());

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        areaError.setColumns(20);
        areaError.setFont(resourceMap.getFont("areaError.font")); // NOI18N
        areaError.setRows(5);
        areaError.setName("areaError"); // NOI18N
        jScrollPane3.setViewportView(areaError);

        jPanel4.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab(resourceMap.getString("jPanel4.TabConstraints.tabTitle"), jPanel4); // NOI18N

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private int curr = 0;
    private int lcurr = 0;
    public double best_work=Double.MAX_VALUE;
    public int[] best_work_patch=null;
    
    public String calcWork(int[] src_in) {
        return calcWork(src_in, false);
    }
    
    public String calcWork(int[] src_in, boolean justcalc) {
        TreeProApp.main.treePanel.clearSolutions();
        LineObject line = null;
        NodeObject node = null;
		if (src_in==null) return "-";
        TreePanel treePanel = TreeProApp.main.treePanel;
        double qMax = 0.0; // Общий вес груза
        String ret="";
        boolean assigned=false;
        int[] src=Arrays.copyOf(src_in, src_in.length);
        ret=("\nПуть: [");
        for (int i =0; i<src.length; i++)
            src[i]+=1;
        
        for (int i=0; i<src.length-1; i++) {
            line = treePanel.findByNumber(src[i], src[i+1]);
            if (line==null) {
                areaError.append("\n[Вывод пути решения] Построение: NULL в объекте line с нодами "+src[i]+" и " +src[i+1]);
                continue;
            }
            node = treePanel.findByNumber(src[i]);
            if (node==null) {
                areaError.append("\n[Вывод пути решения] Расчет: NULL в объекте node "+src[i]);
                continue;
            }
            qMax+=node.volume;
            if (!justcalc) line.setSolution(true);
            if (!assigned) {
                assigned=true;
                if (!justcalc) line.getNo1().setSolution(true);
            }
            ret+=((src[i])+"->");
            line.setName("["+(i+1)+"]");
        }
        ret+=((src[src.length-1])+"]");
        ret+=("\n=================\n");

        double sum = 0.0; // Работа
        double sum_fix = 0.0; // Точная работа
        double q = 0.0;
        double q_fix = 0.0;
        double lenPatch = 0.0; // Путь
        List<Integer> mas = new ArrayList<Integer>();
        

        for (int i=0; i<src.length-1; i++) {
            line = treePanel.findByNumber(src[i], src[i+1]);
            if (line==null) {
                areaError.append("\n[Вывод пути решения] Расчет: NULL в объекте line с нодами "+src[i]+" и " +src[i+1]);
                continue;
            }
            node = treePanel.findByNumber(src[i]);
            if (node==null) {
                areaError.append("\n[Вывод пути решения] Расчет: NULL в объекте node "+src[i]);
                continue;
            }    
            q+=node.volume;
            sum+=(qMax-q)*line.distance;
            
            if (!mas.contains(node.number)) {
                q_fix+=node.volume;
                sum_fix+=(qMax-q_fix)*line.distance;
                mas.add(node.number);
            } else {
                sum_fix+=(qMax-q_fix)*line.distance;
            }
            
            lenPatch+=line.distance;
        }
        
        
        double q_back = 0.0;
        double q_fix_back = 0.0;
        double sum_back = 0.0; // Работа в обратном направлении
        double sum_fix_back = 0.0; // Точная работа в обратном направлении
        mas.clear();
        
        for (int i=src.length-2; i>=0; i--) {
            line = treePanel.findByNumber(src[i], src[i+1]);
            if (line==null) {
                areaError.append("\n[Вывод пути решения] Расчет: NULL в объекте line с нодами "+src[i]+" и " +src[i+1]);
                continue;
            }
            node = treePanel.findByNumber(src[i]);
            if (node==null) {
                areaError.append("\n[Вывод пути решения] Расчет: NULL в объекте node "+src[i]);
                continue;
            }
            q_back+=node.volume;
            sum_back+=(qMax-q_back)*line.distance;
            
            if (!mas.contains(node.number)) {
                q_fix_back+=node.volume;
                sum_fix_back+=(qMax-q_fix_back)*line.distance;
                mas.add(node.number);
            } else {
                sum_fix_back+=(qMax-q_fix_back)*line.distance;
            }
        }
        
        /*
        line = treePanel.findByNumber(src[src.length-1]+1, src[0]+1);
        node = treePanel.findByNumber(src[src.length-1]+1);
        lenPatch+=line.distance;
        sum+=(qMax-q)*line.distance;
        */
        
        ret+=("\nРабота в прямом направлении: ["+sum+"]\n");
        ret+=("Точная работа в прямом направлении: ["+sum_fix+"]\n");
        ret+=("----------------------------------\n");
        ret+=("Работа в обратном направлении: ["+sum_back+"]\n");
        ret+=("Точная работа в обратном направлении: ["+sum_fix_back+"]\n");
        ret+=("----------------------------------\n");
        ret+=("Работа автомобиля: ["+lenPatch*TreeProApp.avtoFreeMassa+"]\n");
        ret+=("Всего груза: ["+qMax+"]\n");
        ret+=("Загрузка автомобиля: ["+( (TreeProApp.avtoMassa>qMax)?("недогруз: "+(TreeProApp.avtoMassa-qMax)):("перегруз: "+(qMax-TreeProApp.avtoMassa) ) )+"]\n");
        ret+=("----------------------------------\n");
        double best_work_tmp=(lenPatch*TreeProApp.avtoFreeMassa+Math.min(sum, sum_back));
        if (best_work_tmp<best_work) {
            best_work=best_work_tmp;
            best_work_patch=Arrays.copyOf(src_in, src_in.length);
        }
        ret+=("Общая работа: ["+best_work_tmp+"]\n");
        
        ret+=("Общая точная работа: ["+(lenPatch*TreeProApp.avtoFreeMassa+Math.min(sum_fix, sum_fix_back))+"]\n");
        ret+=("----------------------------------\n");
        ret+=("Путь: ["+lenPatch+"]\n");
        return ret;
    }
    
    public void refreshResult(int index) {
        //ArrayList<LineObject> res = new ArrayList<LineObject>();
        
        jTextArea1.setText("");
        curr = index;
        if (curr<0)
            curr=TreeProApp.resultPaths.size()-1;
        if (curr>TreeProApp.resultPaths.size()-1)
            curr=0;
        
        if (curr<0||TreeProApp.resultPaths.size()<1) {
            jTextField1.setText("0");
            labelPathOut.setText(" из 0");
            jTextArea1.setText("[Нет решений]");
            return;
        }
        TreePanel treePanel = TreeProApp.main.treePanel;
        int[] src = null;
        try {
            src = Arrays.copyOf(TreeProApp.resultPaths.get(curr), TreeProApp.resultPaths.get(curr).length);
        } catch (Exception e) {
            areaError.append("\n[Вывод пути решения] Копирование массива путей: "+e.getLocalizedMessage());
            return;
        }

        
        treePanel.clearSolutions();
        jTextField1.setText(""+(curr+1));
        labelPathOut.setText(" из "+TreeProApp.resultPaths.size());

        jTextArea1.append(calcWork(src));
    }
    
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        setLocation(TreeProApp.main.getFrame().getLocation().x+TreeProApp.main.getFrame().getWidth()-getWidth(),
                TreeProApp.main.getFrame().getLocation().y+TreeProApp.main.getFrame().getHeight()-getHeight());
        
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        
        setAlwaysOnTop(jToggleButton1.isSelected());
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        TreeProApp.main.treePanel.clearSolutions();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        curr-=1;
        refreshResult(curr);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        curr+=1;
        refreshResult(curr);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        lcurr--;
        refreshLoop(lcurr);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        lcurr++;
        refreshLoop(lcurr);

    }//GEN-LAST:event_jButton7ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        try {
            refreshResult(Integer.parseInt(jTextField1.getText())-1);
        } catch (Exception e) {
            refreshResult(0);
        }
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        try {
            refreshLoop(Integer.parseInt(jTextField2.getText())-1);
        } catch (Exception e) {
            refreshLoop(0);
        }

    }//GEN-LAST:event_jTextField2ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        jButton5.doClick();
        TreeProApp.resultFrame.areaReport.append("\n*****************************************\n");
        TreeProApp.resultFrame.areaReport.append("Предварительный расчет наименьшей работы:\n");
        TreeProApp.main.calcBestWork();
        TreeProApp.resultFrame.areaReport.append("\n*****************************************\n");
    }//GEN-LAST:event_jButton9ActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JTextArea areaError;
    public javax.swing.JTextArea areaLoop;
    public javax.swing.JTextArea areaReport;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JToggleButton jToggleButton1;
    public javax.swing.JLabel labelPathOut;
    public javax.swing.JLabel labelPathOut1;
    // End of variables declaration//GEN-END:variables

    void refreshLoop(int index) {
        lcurr = index;
        if (lcurr<0)
            lcurr=TreeProApp.loopPaths.size()-1;
        if (lcurr>TreeProApp.loopPaths.size()-1)
            lcurr=0;
        
        if (lcurr<0||TreeProApp.loopPaths.size()<1) {
            
            labelPathOut1.setText(" из 0");
            areaLoop.setText("[Нет зависания]");
            jTextField2.setText("0");
            return;
        }
        TreePanel treePanel = TreeProApp.main.treePanel;        
        treePanel.clearSolutions();
        
        BeanLoop beanLoop;
                
        
        
        
        
        try {
            beanLoop = TreeProApp.loopPaths.get(lcurr);
            areaLoop.setText(beanLoop.bugMatrix);
            jTextField2.setText(""+(lcurr+1));
            labelPathOut1.setText(" из "+TreeProApp.loopPaths.size());
        } catch (Exception e) {
            areaError.append("\n[Вывод зависаний] Получение пути: "+e.getLocalizedMessage());
            return;
        }
        
        try {
            treePanel.findByNumber(beanLoop.bugNode+1).setSolution(true);
            int c = 1;
            for (Iterator<Patch> it = beanLoop.paths.iterator(); it.hasNext();) {
                Patch path = it.next();
                LineObject line = treePanel.findByNumber(path.getFrom()+1, path.getTo()+1);
                if (line==null)
                    throw new Exception("Не найден путь из "+ path.getFrom()+1 +" в "+ path.getTo()+1);
                line.setName("["+ c++ +"]");
              
                line.setSolution(true);
            }
            
            
        } catch (Exception e) {
            areaError.append("\n[Вывод зависаний] "+e.getStackTrace().toString());
            return;
        }
        
        
        
    }
}
